package com.atguigu.srb.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.atguigu.srb.base.exception.Assert;
import com.atguigu.srb.base.exception.SrbBussinessException;
import com.atguigu.srb.base.result.ResponseEnum;
import com.atguigu.srb.oss.properties.OssProperties;
import com.atguigu.srb.oss.service.OssService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {
    @Override
    public String upload(MultipartFile file, String module) {
        String filePath= file.getOriginalFilename();//上传文件的名称
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String objectName = module+"/"+new DateTime().toString("yyyy/MM/dd/") + System.currentTimeMillis()+"_"
                + UUID.randomUUID().toString().substring(0,6)+
                filePath.substring(filePath.lastIndexOf("."));
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT,
                OssProperties.ACCESSKEYID, OssProperties.ACCESSKEYSECRET);
        try {
            InputStream inputStream = file.getInputStream();//new FileInputStream(filePath);
            //参数1：桶名，参数2：上传的文件保存到oss桶中的路径+文件名    参数3：要上传的文件
            PutObjectRequest putObjectRequest = new PutObjectRequest(OssProperties.BUCKETNAME,
                    objectName, inputStream);
            // 设置该属性可以返回response。如果不设置，则返回的response为空。
            putObjectRequest.setProcess("true");
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            // 如果上传成功，则返回200。
            System.out.println(result.getResponse().getStatusCode());
            Assert.assertTrue(result.getResponse().getStatusCode()!=200, ResponseEnum.UPLOAD_ERROR);
            //拼接上传后的文件路径
            //https://sh220828-srb.oss-cn-shanghai.aliyuncs.com/imgs/2023/01/12/1673507930341_4209ba.jpg
            String path  =  "https://"+ OssProperties.BUCKETNAME+".oss-cn-shanghai.aliyuncs.com/"+objectName;
            return path;
        } catch (Exception ce) {
            ce.printStackTrace();
            //将编译时的异常捕获转为 自定义异常抛出
            throw new SrbBussinessException(ResponseEnum.UPLOAD_ERROR);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public void delete(String path) {
        String objectName = path.substring(path.indexOf("https://"+OssProperties.BUCKETNAME+".oss-cn-shanghai.aliyuncs.com/")
                + ("https://"+OssProperties.BUCKETNAME+".oss-cn-shanghai.aliyuncs.com/").length());
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT, OssProperties.ACCESSKEYID,
                OssProperties.ACCESSKEYSECRET);
        try {
            // 删除文件或目录。如果要删除目录，目录必须为空。
            ossClient.deleteObject(OssProperties.BUCKETNAME, objectName);
        } catch (Exception ce) {
            throw new SrbBussinessException(ResponseEnum.DELETE_FILE_ERROR);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
